package gsw.tool.skin;

import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import gsw.tool.skin.utils.SkinResources;

import static gsw.tool.skin.SkinAttribute.SKINTYPEFACE;


/**
 * 用来处理view和对应的属性
 */
class SkinView {
    View view;
    List<SkinPair> skinPairs;

    public SkinView(View view, List<SkinPair> skinPairs) {
        this.view = view;
        this.skinPairs = skinPairs;
    }

    /**
     * 换皮肤
     *
     * @param typeface
     */
    public void applySkin(Typeface typeface) {
        applyTypeface(typeface);
        applySkinSupport();

        for (SkinPair skinPair : skinPairs) {
            Drawable left = null, top = null, right = null, bottom = null;
            switch (skinPair.attributeName) {
                case "background":
                    Object background = SkinResources.getInstance().getBackground(skinPair.resID);
                    //如果是Color
                    if (background instanceof Integer) {
                        view.setBackgroundColor((Integer) background);
                    } else {
                        //兼容低版本的写法
                        ViewCompat.setBackground(view, (Drawable) background);
                    }
                    break;
                case "src":
                    background = SkinResources.getInstance().getBackground(skinPair.resID);
                    //如果是Color
                    if (background instanceof Integer) {
                        ((ImageView) view).setImageDrawable(new ColorDrawable((Integer) background));
                    } else {
                        ((ImageView) view).setImageDrawable((Drawable) background);
                    }
                    break;
                case "textColor":
                    ((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList(skinPair.resID));
                    break;
                case "drawableLeft":
                    left = SkinResources.getInstance().getDrawable(skinPair.resID);
                    break;
                case "drawableTop":
                    left = SkinResources.getInstance().getDrawable(skinPair.resID);
                    break;
                case "drawableRight":
                    left = SkinResources.getInstance().getDrawable(skinPair.resID);
                    break;
                case "drawableBottom":
                    left = SkinResources.getInstance().getDrawable(skinPair.resID);
                    break;
                case SKINTYPEFACE:
                    Typeface tf = SkinResources.getInstance().getTypeface(skinPair.resID);
                    applyTypeface(tf);
                    break;
                default:
                    break;
            }

            if (left != null || right != null || top != null || bottom != null) {
                ((TextView) view).setCompoundDrawables(left, top, right, bottom);
            }
        }
    }

    /**
     * 修改自定义控件
     */
    private void applySkinSupport() {
        if (view instanceof SkinViewSupport) {
            ((SkinViewSupport) view).applySkin();
        }
    }

    /**
     * 设置字体
     *
     * @param typeface
     */
    private void applyTypeface(Typeface typeface) {
        if (view instanceof TextView) {
            ((TextView) view).setTypeface(typeface);
        }
    }
}
